package com.ys;

import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.ym.utils.JsonUtil;
import org.junit.Test;

import java.util.Arrays;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.taobao.api.ApiException;
/**
 * @Author 但何非
 * @Date 2022/12/5 13:09
 * @ClassName: DingdingText
 * @Description:
 * @Version:
 */
public class DingdingText {
    private static final String Text = "Text@人员效果";

    /**
     * 实现机器人发送text@人员消息
     */
    public String helloRobots() {
//        System.out.println(JSON.toJSONString(json));
//        String content = json.getJSONObject("text").get("content").toString().replaceAll(" ","");
//        //获取用户userId
//        String userId = json.getString("senderStaffId");
//        String sessionWebhook = json.getString("sessionWebhook");
//        DingTalkClient client = new DefaultDingTalkClient(sessionWebhook);
//        if (Text.equals(content)) {
//            text(client, userId);
//        }
        return null;
    }

    /**
     * 实现@人员
     * @param client
     * @param userId
     */
//    private void text(DingTalkClient client, String userId) {
//        try {
//            OapiRobotSendRequest request = new OapiRobotSendRequest();
//            request.setMsgtype("text");
//            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
//
//            text.setContent(" @" + userId + "  \n  " +
//                    "测试@人员效果");
//            request.setText(text);
//            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
//            System.out.println(userId);
//            at.setAtUserIds(Arrays.asList(userId));
////           isAtAll类型如果不为Boolean，请升级至最新SDK
//            at.setIsAtAll(false);
//            request.setAt(at);
//            OapiRobotSendResponse response = client.execute(request);
//            System.out.println(response.getBody());
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }
//    }

    @Test
    public void a2() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=f74b20530f1bb906f848bfed2ae41222373e7e4318897f2887ee42474a2fbcbf");
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        text.setContent("银盛后台账户余额报警:当前余额￥");
        request.setText(text);
//        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
//        at.setAtMobiles(Arrays.asList("132xxxxxxxx"));
//// isAtAll类型如果不为Boolean，请升级至最新SDK
//        at.setIsAtAll(true);
//        at.setAtUserIds(Arrays.asList("109929","32099"));
//        request.setAt(at);

//        request.setMsgtype("link");
//        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
//        link.setMessageUrl("https://www.dingtalk.com/");
//        link.setPicUrl("");
//        link.setTitle("时代的火车向前开");
//        link.setText("这个即将发布的新版本，创始人xx称它为红树林。而在此之前，每当面临重大升级，产品经理们都会取一个应景的代号，这一次，为什么是红树林");
//        request.setLink(link);

//        request.setMsgtype("markdown");
//        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
//        markdown.setTitle("杭州天气");
//        markdown.setText("#### 杭州天气 @156xxxx8827\n" +
//                "> 9度，西北风1级，空气良89，相对温度73%\n\n" +
//                "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n"  +
//                "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
//        request.setMarkdown(markdown);
        OapiRobotSendResponse response = client.execute(request);
        System.err.println(JsonUtil.toJson(response));
        if(response.isSuccess()){
            System.err.println(111111111111111111l);
        }
    }
}
